package cn.huijielong.modules.sys.controller;

import cn.huijielong.common.utils.RD;
import cn.huijielong.common.utils.HttpContextUtils;
import cn.huijielong.common.utils.PageUtils;
import cn.huijielong.modules.pub.entity.MerchantEntity;
import cn.huijielong.modules.pub.service.MerchantService;
import cn.huijielong.modules.sys.entity.SysUserEntity;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.util.StringUtil;

/**
 * Controller公共组件
 * 
 * @date 2016年11月9日 下午9:42:26
 */
public abstract class AbstractController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	protected MerchantService merchantService;
	
	protected MerchantEntity getMerchant() {
		Long merchantId=MerchantEntity.getMerchantIdByRequest();
		if(merchantId==null || merchantId.longValue()==0) {
			return new MerchantEntity();
		}
		return merchantService.queryObject(merchantId);
	}
	
	protected SysUserEntity getUser() {
		return (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
	}

	protected Long getUserId() {
		return getUser().getUserId();
	}
	
	protected String getHeader(String name) {
		return HttpContextUtils.getHttpServletRequest().getHeader(name);
	}
	protected RD getData(String dataKey,List<?> data) {
		RD r=RD.ok();
		if("page".equals(dataKey)){
			PageUtils pageUtil = new PageUtils(data, data.size(), 30, 1);
			r.put("page", pageUtil);
		}else if(StringUtil.isNotEmpty(dataKey)){
			r.put(dataKey, data);
		}else {
			r.setData(data);
		}
		return r;
	}
	protected Object getObject(String dataKey,Object data) {
		if("page".equals(dataKey)){
			return data;
		}else {
			return RD.ok().setData(data);
		}
	}
}
